
Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    var ordersrow;
    var ticketsrow;
    
    var beginDate = new Ext.form.DateField({
        fieldLabel: '订单日期从',
        name: 'begin_date',
        id: 'begin_date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        blankText: '开始日期必须选择',
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //applyTo: 'created_date',
        //anchor: '50%'
        vtype: 'daterange',
        endDateField: 'end_date',
        width: 100
    //,value: today
    })
    var endDate = new Ext.form.DateField({
        fieldLabel: '到',
        name: 'end_date',
        id: 'end_date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        blankText: '截止日期必须选择',
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //applyTo: 'created_date2',
        //anchor: '50%'
        vtype: 'daterange',
        startDateField: 'begin_date',
        width: 100
    //,value: today
    })
    var queryButton = new Ext.Button({
        text: '查 询',
        handler: function(){
            if (formPanel.form.isValid()) {
                noCustomer_orderds.baseParams.type = "noCustomer";
                noCustomer_orderds.baseParams.created_date = beginDate.getValue().format('Y-m-d');
                noCustomer_orderds.baseParams.created_date_to = endDate.getValue().format('Y-m-d');
                noCustomer_orderds.reload();
                noCustomer_ticketds.baseParams.id = "";
                noCustomer_ticketds.reload();
            }
        }
    })
    var formPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 70,
        width: '100%',
        frame: true,
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [beginDate]
            }, {
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [endDate]
            }, {
                columnWidth: .4,
                layout: 'form',
                border: false,
                items: [queryButton]
            }]
        }]
    });
    formPanel.render('noCustomer_query_form');
    
    //############## 订单Grid ##############
    var ordercm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id'
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 100
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 70
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 200
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 70
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 100
    }, {
        header: '预定人',
        dataIndex: 'creator',
        width: 70
    }, {
        header: '预定时间',
        dataIndex: 'created_at',
        width: 120,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '出票人',
        dataIndex: 'ticket_outer',
        width: 70
    }, {
        header: '出票时间',
        dataIndex: 'ticket_out_date',
        width: 120,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '送票人',
        dataIndex: 'sender',
        width: 70
    }, {
        header: '送票时间',
        dataIndex: 'send_date',
        width: 100
    }, {
        header: '收款人',
        dataIndex: 'cash_checker',
        width: 70
    }, {
        header: '收款时间',
        dataIndex: 'cash_get_date',
        width: 120,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    
    }]);
    
    //隐藏order数据库id
    ordercm.setHidden(1, !ordercm.isHidden(1));
    var noCustomer_orderds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getOrdersBySearch?type=noCustomer'
        }),
        reader: orderReader
    });
    var ordergridpanel = new Ext.grid.GridPanel({
        el: 'noCustomer_ordersgrid',
        ds: noCustomer_orderds,
        cm: ordercm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 310,
        title: '订单信息'
    });
    ordergridpanel.render();
    noCustomer_orderds.load();
    
    //双击订单查看机票详细
    ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
        var current_order_selected = noCustomer_orderds.data.items[rowIndex].id;
        ordersrow = noCustomer_orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        noCustomer_ticketds.baseParams.id = current_order_id;
        noCustomer_ticketds.reload();
    });

    //############ 订单-右键菜单 ##########
    var changeCustomerItem = new Ext.menu.Item({
        text: '【订单转换】',
        //iconCls: 'album',
        handler: rightMenu0Fn
    });
    var editOrderItem = new Ext.menu.Item({
        text: '【修改订单】',
        //iconCls: 'album',
        handler: rightMenu1Fn
    });
    var cancelOrderItem = new Ext.menu.Item({
        text: '【取消订单】',
        //iconCls: 'album',
        handler: rightMenu2Fn
    });
    var rightClick = new Ext.menu.Menu({
        items: [changeCustomerItem, '-', editOrderItem, cancelOrderItem]
    });
    function rightMenu0Fn(){
        //Ext.MessageBox.alert('操作提示', '指定会员！');
        changeCustomer(noCustomer_orderds, current_order_id, noCustomer_ticketds);
    }
    function rightMenu1Fn(){
        ModOrder(noCustomer_orderds, current_order_id, noCustomer_ticketds);
    }
    function rightMenu2Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, noCustomer_orderds, noCustomer_tickets);
    }
    //订单右键菜单
    ordergridpanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = noCustomer_orderds.data.items[rowIndex].id;
        ordersrow = noCustomer_orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        if (current_order_status==ORDER_STATUS_YCP || current_order_status==ORDER_STATUS_YFK || current_order_status==ORDER_STATUS_YQX || current_order_status==ORDER_STATUS_YZF || current_order_status==ORDER_STATUS_YTD){
            cancelOrderItem.disable();
            editOrderItem.disable();
        }else{
            cancelOrderItem.enable();
            editOrderItem.enable();
        }
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });    
    //### 机票Grid ###
    var noCustomer_ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id'
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 100
    }, {
        header: '状态',
        dataIndex: 'status',
        width: 70
    }, {
        header: '出票状态',
        dataIndex: 'status_type',
        sortable: true,
        width: 70
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 70
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 50
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 70
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 70
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 70
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 50
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 70
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 70
    
    }]);
    
    //隐藏ticket数据库id
    noCustomer_ticketcm.setHidden(1, !noCustomer_ticketcm.isHidden(1));
    var ticketProxy = new Ext.data.HttpProxy({
        url: '/orders/getTicketsByOrderID?showCancelTicket=no'
    });
    
    var noCustomer_ticketds = new Ext.data.Store({
        proxy: ticketProxy,
        reader: ticketReader
    });
    
    var ticketgridpanel = new Ext.grid.GridPanel({
        el: 'noCustomer_ticketsgrid',
        ds: noCustomer_ticketds,
        cm: noCustomer_ticketcm,
        //sm: sm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 160,
        title: '航班信息'
    });
    ticketgridpanel.render();
    noCustomer_ticketds.load();
    
    //############ 票-右键菜单 ##########
    var editTicketItem = new Ext.menu.Item({
        text: '【编辑修改】',
        //iconCls: 'album',
        handler: ticketRightMenu1
    });
    var delPassengerItem = new Ext.menu.Item({
        text: '【取消乘客】',
        //iconCls: 'album',
        handler: ticketRightMenu2
    });
    function ticketRightMenu1(){
        ModTicket(noCustomer_ticketds, current_ticket_id, noCustomer_orderds);
    };
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
        cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, noCustomer_ticketds);
    };
    var ticketRightClick = new Ext.menu.Menu({
        items: [editTicketItem, delPassengerItem]
    });
    ticketgridpanel.on('rowcontextmenu', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = noCustomer_ticketds.data.items[rowIndex].id;
        ticketsrow = noCustomer_ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        if (current_ticket_status==TICKET_STATUS_YQX || current_ticket_status==TICKET_STATUS_YZF || current_ticket_status==TICKET_STATUS_YTP){
            //已取消 已作废 已退票
            delPassengerItem.disable();
            editTicketItem.disable();
        }else{
            delPassengerItem.enable();
            editTicketItem.enable();
        }
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
})
